<template>
  <common-layout>
    <div class="login">
      <a-form v-if="mode === 'login'" @submit="onSubmit" :form="form">
        <div class="title">登 录</div>
        <a-form-item label="用户名">
          <a-input
            autocomplete="autocomplete"
            size="large"
            placeholder="请输入用户名"
            v-decorator="[
              'username',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入账户名',
                    whitespace: true,
                  },
                ],
              },
            ]"
          >
            <a-icon slot="prefix" type="user" />
          </a-input>
        </a-form-item>
        <a-form-item label="密码">
          <a-input
            size="large"
            placeholder="请输入密码"
            autocomplete="autocomplete"
            type="password"
            v-decorator="[
              'password',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入密码',
                    whitespace: true,
                  },
                ],
              },
            ]"
          >
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-button
            :loading="logging"
            style="width: 100%;margin-top: 24px"
            size="large"
            htmlType="submit"
            type="primary"
            >登录</a-button
          >
        </a-form-item>
        <div style="text-align: center;font-size: 12px;">
          没有账号?前往<span
            class="hover-span"
            @click="mode = 'sign'"
            >注册</span
          >
        </div>
      </a-form>
      <a-form v-if="mode === 'sign'" @submit="onSubmit" :form="formSign">
        <div class="title">注 册</div>
        <a-form-item label="用户名">
          <a-input
            autocomplete="autocomplete"
            size="large"
            placeholder="请输入用户名"
            v-decorator="[
              'username',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入用户名',
                    whitespace: true,
                  },
                ],
              },
            ]"
          >
            <a-icon slot="prefix" type="user" />
          </a-input>
        </a-form-item>
        <a-form-item label="密码">
          <a-input
            size="large"
            placeholder="请输入密码"
            autocomplete="autocomplete"
            type="password"
            v-decorator="[
              'password',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入密码',
                    whitespace: true,
                  },
                ],
              },
            ]"
          >
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>
        <a-form-item label="邮箱">
          <a-input
            autocomplete="autocomplete"
            size="large"
            placeholder="请输入邮箱"
            v-decorator="[
              'email'
            ]"
          >
            <a-icon slot="prefix" type="mail" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-button
            :loading="logging"
            style="width: 100%;margin-top: 24px"
            size="large"
            htmlType="submit"
            type="primary"
            >注册</a-button
          >
        </a-form-item>
        <div style="text-align: center;font-size: 12px;">
          已有账户?返回<span
            style="color:#722ed1;margin-left: 4px;cursor: pointer;"
            @click="mode = 'login'"
            >登录</span
          >
        </div>
      </a-form>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from "@/layouts/CommonLayout";
import { mapMutations } from "vuex";
import { register, login } from '../../service';

export default {
  name: "Login",
  components: { CommonLayout },
  data() {
    return {
      logging: false,
      mode: "login",
      form: this.$form.createForm(this),
      formSign: this.$form.createForm(this),
    };
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
  methods: {
    ...mapMutations("account", ["setUser", "setPermissions", "setRoles"]),
    onSubmit() {
      if (this.mode === 'login') {
        login({
          username: this.form.getFieldValue("username"),
          password: this.form.getFieldValue("password")
        }).then(res => {
          if (res.code == 200) {
            localStorage.setItem('medicalUserInfo', JSON.stringify(res.data))
            localStorage.setItem(process.env.VUE_APP_USER_KEY, JSON.stringify({
              user: res.data.username
            }))
            localStorage.setItem('medicalUserInfo', JSON.stringify(res.data))
            this.setUser(res.data.username);
            this.$router.push("/page/dashboard");
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        register({
          username: this.formSign.getFieldValue("username"),
          password: this.formSign.getFieldValue("password"),
          email: this.formSign.getFieldValue("email"),
        }).then(res => {
          if (res.code == 200) {
            this.$message.success('注册成功，请登录')
            this.mode = 'login'
            this.formSign.resetFields();
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    }
  },
};
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 40px;
        color: @title-color;
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 480px;
    margin: 0 auto;
    background: #fff;
    border-radius: 12px;
    border: 1px solid #f3f3f3;
    padding: 50px 44px;

    .title {
      width: 100%;
      font-size: 20px;
      text-align: center;
      color: #722ed1;
      line-height: 40px;
      font-weight: bold;
    }
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
.hover-span {
  color: #722ed1;
  margin-left: 4px;
  cursor: pointer;
}
</style>
